<template>
  <RouterView>
    <template #default="{ Component, route }">
      <!--      <transition-->
      <!--        :name="-->
      <!--          getTransitionName({-->
      <!--            route,-->
      <!--            openCache,-->
      <!--            enableTransition: getEnableTransition,-->
      <!--            cacheTabs: getCaches,-->
      <!--            def: getBasicTransition,-->
      <!--          })-->
      <!--        "-->
      <!--        mode="out-in"-->
      <!--        appear-->
      <!--      >-->
      <keep-alive v-if="openCache" :include="getCaches">
        <component :is="Component" :key="route.fullPath" />
      </keep-alive>
      <component v-else :is="Component" :key="route.fullPath" />
      <!--      </transition>-->
    </template>
  </RouterView>
  <FrameLayout v-if="getCanEmbedIFramePage" />
</template>

<script lang="ts">
import { computed, defineComponent, unref, onBeforeMount, onBeforeUnmount, ref } from 'vue';
import FrameLayout from '/@/layouts/iframe/index.vue';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
import { getTransitionName } from './transition';
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
// import { useNurseStore } from '/@/store/modules/nurse';
// import { connectWebSocket as connectNurseWebSocket, onWebSocket as onNurseWebSocket } from '/@/hooks/web/useWebSocket';
// import useEmitter from '/@/utils/use-emitter';
import useRequest from 'vue-hooks-plus/es/useRequest'
// import { defHttp } from '/@/utils/http/axios';
// import { getToken } from '/@/utils/auth';
// import md5 from 'crypto-js/md5';
// import { useUserStore } from '/@/store/modules/user';
// import { useGlobSetting } from '/@/hooks/setting';
// import useNurseWebSocket from './nurse-websocket';

export default defineComponent({
  name: 'PageLayout',
  components: { FrameLayout },
  setup() {
    // const glob = useGlobSetting();

    // const userStore = useUserStore();

    // 初始化 initNUrseWebSocket
    // function initNurseWebSocket() {
    //   let token = getToken();
    //   //将登录token生成一个短的标识
    //   let wsClientId = md5(token);
    //   let userId = unref(userStore.getUserInfo).id + "_" + wsClientId;
    //   // WebSocket与普通的请求所用协议有所不同，ws等同于http，wss等同于https
    //   let url = glob.domainUrl?.replace('https://', 'wss://').replace('http://', 'ws://') + '/nurse/websocket/' + userId;

    //   const webSocket = connectNurseWebSocket(url);

    //   onNurseWebSocket(onNurseWebSocketMessage);

    //   return webSocket
    // }
    // function onNurseWebSocketMessage(data) {
    //   if (data?.msg === 'refundOrder') {
    //     notification.warn({ message: "您有新的退款申请,请及时处理！", duration: 5 })
    //     try {
    //       fetch("http://127.0.0.1:3007/refundOrder")
    //     } catch (error) {
    //     }
    //   }

    //   if (data?.msg === 'newOrder') {
    //     notification.warn({ message: "您有新的订单,请及时处理！", duration: 5 })
    //     try {
    //       fetch("http://127.0.0.1:3007/newOrder")
    //     } catch (error) {
    //     }

    //   }
    //   nurseEmitter.$emit("nurseEmitter-refresh", data)
    // }

    // const { setWebSocket, getWebSocket } = useNurseStore()

    // const { initNurseWebSocket, close } = useNurseWebSocket()

    // const nurseEmitter = useEmitter()

    // const order = ref({ newOrder: String(new Date().getTime()), refundOrder: String(new Date().getTime()) })

    const { getShowMultipleTab } = useMultipleTabSetting();

    const tabStore = useMultipleTabStore();

    const { getOpenKeepAlive, getCanEmbedIFramePage } = useRootSetting();

    const { getBasicTransition, getEnableTransition } = useTransitionSetting();

    const openCache = computed(() => unref(getOpenKeepAlive) && unref(getShowMultipleTab));

    const getCaches = computed((): string[] => {
      if (!unref(getOpenKeepAlive)) {
        return [];
      }
      return tabStore.getCachedTabList;
    });

    // async function refundOrder() {
    //   const { timestamp } = await defHttp.get({
    //     url: `/nurse_user_orders/nurseUserOrders/refundOrder?timestamp=${order.value.refundOrder}`
    //   })
    //   if (timestamp && timestamp !== order.value.refundOrder) {
    //     nurseEmitter.$emit("nurseEmitter-refresh-refundOrder")
    //     order.value.refundOrder = timestamp
    //   }

    // }
    // async function newOrder() {
    //   const { timestamp } = await defHttp.get({
    //     url: `/nurse_user_orders/nurseUserOrders/newOrder?timestamp=${order.value.newOrder}`
    //   })
    //   if (timestamp && timestamp !== order.value.newOrder) {

    //     nurseEmitter.$emit("nurseEmitter-refresh-newOrder")
    //     order.value.newOrder = timestamp
    //   }
    // }
    // const time = ref(6000)
    // const computedTime = computed(() => time.value + 100)
    const { run, cancel } = useRequest(() => refresh(), {
      manual: true,
      pollingInterval: 6000,
      pollingWhenHidden: false,
      pollingErrorRetryCount: 1
    })
    async function refresh() {
      // await refundOrder()
      // await newOrder()
    }

    onBeforeMount(() => {
      run()
    })
    onBeforeUnmount(() => {
      cancel()
    })

    return {
      getTransitionName,
      openCache,
      getEnableTransition,
      getBasicTransition,
      getCaches,
      getCanEmbedIFramePage,
    };
  },
});
</script>
